<?php
/**
 * Created by PhpStorm.
 * User: jiayanxiu
 * Date: 15/9/16
 * Time: 18:10
 */
class RegionApp extends MallbaseApp {


    /**
     * 获取城市地址列表
     * @author jyx
     * @date 2015/09/16
     */
    public function index() {
        $action = '获取城市地址列表';
        setLogParameter($action);
        $this->check_method('GET');
        $model = m('region');
        $list = array();
        foreach ($model->get_list() as $data) {
            $datas [$data ['region_id']] = $data;       //以region_id为键值的一维数组
            $tree [$data ['parent_id']] [] = $data;     //以parent_id为一组的二维数组
        }
        foreach ($datas as $region) {
            // 根
            if ($region ['parent_id'] == 0) {
                $list [$region ['region_id']] = $region;
                // 节点级别的叶子 例如 港澳台
            } else if ($datas [$region['parent_id']] ['parent_id'] == 0 and !$tree [$region ['region_id']]) {       //判断是否是城市
                $list [$region ['parent_id']] ['sub'][$region['region_id']] = $region;
                // 节点
            } else if ($tree [$region ['region_id']]) {         //判断是不是这个城市里面的
                $list [$region ['parent_id']] ['sub'][$region['region_id']] = $region;
            } else {
                // 找到根
                $root = $datas [$region ['parent_id']]['parent_id'];
                // 找到节点 (父系)
                $node = $region ['parent_id'];
                $list [$root] ['sub'] [$node] ['sub'] [] = $region;
            }
        }
        //数组里面还有不是0的键值，要去除掉
        $list = array_values($list);
        foreach ($list as &$temp) {
            if ($temp ['sub']) {
                $temp ['sub'] = array_values($temp ['sub']);
            }
        }
        $this->ajax_return(FLAG_OK, '', $list);

    }







}